/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import events.LoadXmlEvent;
import events.LoadXmlEventHandler;
import events.StopEvent;
import events.StopEventHandler;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import panes.FileLoaderPane;
import simpSync.SimpSyncParser;
import simpSync.model.Link;
import simpSync.model.Media;
import simpSync.model.Region;
import simpSync.model.SimpleSync;

/**
 * Classe principal de todo o projeto
 */
public class playersimpsync extends Application {

    SimpleSync ss;
    Pane emptyPane;

    @Override
    public void start(Stage primaryStage) {

        //Node Raiz
        VBox root = new VBox();

        //Painel do topo com o caminho do XML para carregar
        FileLoaderPane fileLoaderPane = new FileLoaderPane();
        root.getChildren().add(fileLoaderPane);

        //Evento quando clica no botão carregar
        fileLoaderPane.addEventFilter(LoadXmlEvent.LOAD_XML, new LoadXmlEventHandler() {
            public void handle(LoadXmlEvent event) {
                SimpSyncParser ssp = new SimpSyncParser();
                ss = ssp.parse(event.getPath());
                execute();
            }
        });

        //Evento quando clica no botão carregar
        fileLoaderPane.addEventFilter(StopEvent.STOP, new StopEventHandler() {
            public void handle(StopEvent event) {
                emptyPane.getChildren().clear();
                System.out.println("Tentando parar tudo.");
            }
        });

        emptyPane = new Pane();
        emptyPane.setStyle("-fx-background-color: #ffffff;");
        root.getChildren().add(emptyPane);
        root.setVgrow(emptyPane, Priority.ALWAYS);


        Scene scene = new Scene(root, 1280, 768);
        primaryStage.setTitle("SimpleSinc Player - Daniel/Anderson");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /*
     * Método para executar as informações do XML. Ele desenha as regiões, 
     * cria as mídias e associa os links.
     */
    public void execute() {
        for (Region r : ss.getRegions()) {
            r.draw(emptyPane);
        }

        for (Media m : ss.getMedias()) {
            m.create(emptyPane, ss);
        }

        for (Link l : ss.getLinks()) {
            l.assign(emptyPane,ss);
        }

    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
